//
//  ChooseTheRightColorGameScene.cpp
//  CanYouDoIt
//
//  Created by hoangtx on 7/13/13.
//
//

#include "ChooseTheRightColorGameScene.h"
#include "GameLayerManager.h"
#include "SceneManager.h"
#include "StaticVariables.h"
CCScene* ChooseTheRightColorGameScene::scene(int numberOfSeconds){
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    ChooseTheRightColorGameScene *layer = ChooseTheRightColorGameScene::create(numberOfSeconds);
    GameLayerManager::sharedInstance()->chooseTheRightColorGameLayer = layer;
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}


bool ChooseTheRightColorGameScene::init(){
    ColorPickerGameScene::init();
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    double labelSize = winSize.width*0.25;
    targetLabel = CCLabelBMFont::create("red", "Info.fnt");
    targetLabel->setAnchorPoint(ccp(0.5,0.5));
    targetLabel->setScale(labelSize/targetLabel->getContentSize().height);
    targetLabel->setPosition(ccp(winSize.width/2,(winSize.height-scoreAndTimeLayer->getBarHeight())*0.75f));
    targetLabel->setVisible(false);
    this->addChild(targetLabel);
    
    textArray = CCArray::create();
    textArray->retain();
    textArray->addObject(CCString::create("red"));
    textArray->addObject(CCString::create("green"));
    textArray->addObject(CCString::create("blue"));
    

    
    colorArray[0]=ccc3(255,0,0);
    colorArray[1]=ccc3(0,255,0);
    colorArray[2]=ccc3(0,0,255);
    switchColor();
    return true;
}

ChooseTheRightColorGameScene* ChooseTheRightColorGameScene::create(int numberOfSeconds){
    ChooseTheRightColorGameScene* scene = ChooseTheRightColorGameScene::create();
    scene->maxTimeCountDown = numberOfSeconds;
    scene->timeRemaining = scene->maxTimeCountDown;
    scene->scoreAndTimeLayer->updateTime(scene->timeRemaining);
    return scene;
}
void ChooseTheRightColorGameScene::switchColor(){
    int textRandom;
    // = rand()%3;
    do {
        textRandom = rand()%3;
    } while (textRandom==currentCorrectColor);
    int colorRandom;
    do {
        colorRandom = rand()%3;
    } while (colorRandom == textRandom);
    CCString* textString = (CCString*)textArray->objectAtIndex(textRandom);
    targetLabel->setString(textString->getCString());
    targetLabel->setColor(colorArray[colorRandom]);
    currentCorrectColor = textRandom;
    
}

void ChooseTheRightColorGameScene::startGame(){
    ColorPickerGameScene::startGame();
    targetLabel->setVisible(true);
    SceneManager::sharedInstance()->previousScene = SceneManager::sharedInstance()->chooseTheRightColorGameScene;
}

void ChooseTheRightColorGameScene::reset(){
    ColorPickerGameScene::reset();
    targetLabel->setVisible(false);
    switchColor();
}



void ChooseTheRightColorGameScene::onTapButton(cocos2d::CCObject *sender){
    int pressIndex = ((CCInteger*)sender)->getValue();

    if(pressIndex==currentCorrectColor){
        switchColor();
        numberOfTap++;
        scoreAndTimeLayer->updateScore(numberOfTap);
    }else{
        gameOver();
    }
}